package com.liang.common;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.http.HttpStatus;

import java.io.Serializable;

/**
 * @author liangben
 * @date 12/4/2022 下午 7:40
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result implements Serializable {

    //是否成功
    private boolean success;

    //返回码
    private int code;

    //返回信息
    private String message;

    //返回数据
    private Object data;

    public static Result buildSuccess() {
        return buildSuccessMsg("操作成功");
    }

    public static Result buildSuccessData(Object data) {
        return new Result(true, HttpStatus.OK.value(), "操作成功", data);
    }

    public static Result buildSuccessMsg(String msg) {
        return new Result(true, HttpStatus.OK.value(),
                msg, String.valueOf(System.currentTimeMillis()));
    }

    public static Result buildSuccess(String msg, Object data) {
        return new Result(true, HttpStatus.OK.value(),
                msg, data);
    }

    public static Result buildFail() {
        return buildFailMsg("操作失败");
    }

    public static Result buildFailMsg(String msg) {
        return buildFail(HttpStatus.BAD_REQUEST.value(), msg);
    }

    public static Result buildFail(Integer code, String msg) {
        return new Result(false, code, msg, String.valueOf(System.currentTimeMillis()));
    }
}
